package cn.lyh.FoodSystem.Service.ImpI;


import cn.lyh.FoodSystem.Dao.VideoDao;
import cn.lyh.FoodSystem.Dto.PageInfo;
import cn.lyh.FoodSystem.Dto.Result;
import cn.lyh.FoodSystem.Entity.Video;
import cn.lyh.FoodSystem.Service.VideoService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class VideoServiceImpI  implements VideoService {

    private final VideoDao videoDao;

    public VideoServiceImpI(VideoDao videoDao) {
        this.videoDao = videoDao;
    }


    /**
     * 添加视频
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED )
    public Result addVideo(Video video){
        Result result = new Result();
        try {
          if(video.getName()!=null||video.getType()!=null||video.getUrl()!=null){
             Video video1 =videoDao.save(video);
              if (video1.getId()!=null){
                  result.setCode(200);
                  result.setMsg("添加成功！");
              }else{
                  result.setCode(400);
                  result.setMsg("添加失败！");
              }
          }else {
              result.setCode(400);
              result.setMsg("参数不能为空！");
          }
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("程序错误！");
            result.setData(e.getCause());
        }
        return result;
    }

    /**
     * 查询视频
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED )
    public Result queryVideo(PageInfo pageInfo){
        Result result = new Result();
        try {
            Pageable pageable = PageRequest.of(pageInfo.getPage(), pageInfo.getLimit());
            Page<Video> videos  =videoDao.findAll(pageable);
         result.setCode(200);
         result.setMsg("视频查询成功");
         result.setData(videos);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("程序错误！");
            result.setData(e.getCause());
        }
        return result;
    }

    /**
     * 删除视频
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED )
    public Result deleteVideo(Video video){
        Result result = new Result();
        try {

        }catch (Exception e){
            result.setCode(500);
            result.setMsg("程序错误！");
            result.setData(e.getCause());
        }
        return result;
    }
}
